
require "sketchup.rb"
require "EventRelay.dll"

# ----------------------------------------------------------------------------
class MyEventRelay < EventRelay
# ----------------------------------------------------------------------------
    # Virtual key codes
    VK_F1     = 112 #F1 key
    VK_LCTRL  = 308 #LCtrl key
    VK_LSHIFT = 306 #LCtrl key
    VK_CONTROL = 17

    VK_A = 65 #a key
    VK_Y = 89 #y key
    VK_M = 77 #m key
    VK_Z = 90 #z key
    SUPPRESS = true
    ALLOW = false

    # ----------------------------------
    #    initialize
    # ----------------------------------
    def initialize
        super
        @handDolly = false
        @toolsObserver = MyToolsObserver.new
        Sketchup.active_model.tools.add_observer(@toolsObserver)
        #-SendKeyMacro("!WR") #// Open Ruby Console
    end
    # ----------------------------------
    #    OnKeyDown
    # ----------------------------------
    def OnKeyDown(key,repeat,flags)
        #-puts "rb file overrode OnKeyDown"
        #-Logit "FROM RUBY OnKeyDown key:#{key} repeat:#{repeat} flags:#{flags}"

        #-vk_key, keyMacro = GetKeyInfo(key, flags)
        #-Logit("FROM RUBY: vk_key:#{vk_key} keyMacro:{#{keyMacro}}")
        #--puts("FROM RUBY: vk_key:#{vk_key} keyMacro:{#{keyMacro}}")

        # Kill F1 key
        if (key == VK_F1) #Kill Help key
            return SUPPRESS
        end

        return ALLOW
    end
    # ----------------------------------
    #    OnKeyUp
    # ----------------------------------
    def OnKeyUp(key,repeat,flags)
        #-Logit "FROM RUBY OnKeyUp key:#{key} repeat:#{repeat} flags:#{flags}"
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnLButtonDown
    # ----------------------------------
    def OnLButtonDown(flags, x, y)
        #-puts "OnLButton DOWN"
        toolis = @toolsObserver.getActiveTool()
		#-puts("EventRelay::OnLButtonDown ToolsObserver:#{toolis}")

        # MoveTool gets a Ctrl key to cause Copy mode
        if ( (toolis == "MoveTool") && (@toolsObserver.getToolState() == 0) )
            #-Logit("OnLButtonDown Sending {LCTRL}")
            SendKeyMacro("{LCTRL}") # put MoveTool in Copy mode
        end
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnLButtonUp
    # ----------------------------------
    def OnLButtonUp(flags, x, y)
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnRButtonUp
    # ----------------------------------
    def OnRButtonUp(flags, x, y)
        #-Logit "FROMRUBY: OnRButtonUp flags:#{flags} x:#{x} y:#{y}"

        # Release the Camera Dolly after RMouseButton dragging
        if ( @handDolly )
            @handDolly = false
            SendKeyMacro("{MBUTTON UP}{LSHIFT UP}")
            return SUPPRESS
        end
        return ALLOW
    end #OnRButtonUp
    # ----------------------------------
    #    Mouse OnRButtonDown
    # ----------------------------------
    def OnRButtonDown(flags, x, y)
        #-Logit "FROMRUBY: OnRButtonDown flags:#{flags} x:#{x} y:#{y}"

        # Right Mouse dragging == Pan (Hand tool)
        x1, y1 = GetMousePosition()
        sleep(0.200)
        x2, y2 = GetMousePosition()
        if ( ((x2-x1).abs > 2) or ((y2-y1).abs > 2) )
            #-Logit "FROMRUBY Mouse DRAGGING"
            SendKeyMacro( "{LSHIFT DOWN}{MBUTTON DOWN}" )
            @handDolly = true
            return SUPPRESS
        end
        return ALLOW
    end #OnRButtonDown
    # ----------------------------------
    #    Menus
    # ----------------------------------
    def OnMenuSelected(menuId, menuLabel)
        #-Logit "FROMRUBY: OnMenuSelected MenuId:#{menuId} label:#{menuLabel}"
        @toolsObserver.checkToolsObserver()
        #-if (menuId == 57670) #Kill Help key
        #- See alternative OnKeyDown F1 suppression also
        #-    return SUPPRESS
        #-end

        return ALLOW
    end #OnMenuSelected

end # class MyEventRelay
# ----------------------------------------------------------------------------
class MyToolsObserver < Sketchup::ToolsObserver
# ----------------------------------------------------------------------------
    # We need a Tools Observer to tell us when the MoveTool
    # is active.

    def initialize
        super
        @currentModelId = Sketchup.active_model.object_id
        @currentTool = "notool"
        @currentToolState = 0
    end

    def onActiveToolChanged(tools_object, toolname, toolid)
        @currentTool = toolname
        #-puts "Active Tool Changed: #{@currentTool}"
        #-anEventRelay.Logit("New Active Tool: #{@currentTool}")
    end
    def onToolStateChanged(tools_object, toolname, toolid, state)
        @currentTool = toolname
        @currentToolState = state
        #-puts "Tool State Changed: #{@currentTool} #{state}"
    end

    def checkToolsObserver
        if ( @currentModelId != Sketchup.active_model.object_id)
            #-printf("old[%d] new[%d]\n", @currentModelId, Sketchup.active_model.object_id)
            Sketchup.active_model.tools.remove_observer(self) #// probably not necessary
            @currentModelId = Sketchup.active_model.object_id
            Sketchup.active_model.tools.add_observer(self)
        end
    end

    def getActiveTool
        return @currentTool
    end

    def getToolState
        return @currentToolState
    end

end # class MyToolsObserver
# ----------------------------------------------------------------------------

unless file_loaded?("EventRelay.rb")
    anEventRelay = MyEventRelay.new
    file_loaded "EventRelay.rb"
end

